package com.nexgo.payment.trans.setting.transmanage;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.nexgo.payment.R;
import com.nexgo.payment.constrant.PreferencesConstants;
import com.nexgo.payment.constrant.SysConstant;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.trans.setting.SaveParameterActivity;
import com.nexgo.payment.trans.setting.SaveParameterPresenter;
import com.nexgo.payment.util.StringUtils;
import com.nexgo.payment.util.UIUtils;
import com.nexgo.payment.widget.AmountTextWatcher;
import com.nexgo.payment.widget.SwitchButton;
import com.nexgo.payment.widget.TitleBuilder;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 交易管理下的其他参数设置
 */
public class SetupOtherSwitchActivity extends SaveParameterActivity {

    @BindView(R.id.offline_trans_upload_number)
    EditText offlineTransUploadNumber;
    @BindView(R.id.offline_trans_upload_times)
    EditText offlineTransUploadTimes;
    @BindView(R.id.max_refund_amount)
    EditText maxRefundAmount;
    @BindView(R.id.void_need_password)
    SwitchButton voidNeedPassword;
    @BindView(R.id.pre_auth_void_need_password)
    SwitchButton preAuthVoidNeedPassword;
    @BindView(R.id.pre_auth_complete_need_password)
    SwitchButton preAuthCompleteNeedPassword;
    @BindView(R.id.pre_auth_complete_void_need_password)
    SwitchButton preAuthCompleteVoidNeedPassword;
    @BindView(R.id.allow_manual_pan)
    SwitchButton allowManualPan;
    @BindView(R.id.confirm_pan)
    SwitchButton confirmPan;
    @BindView(R.id.reversal_upload_times)
    EditText reversalUploadTimes;
    @BindView(R.id.check_gps)
    SwitchButton checkGps;
    @BindView(R.id.rl_need_check_base)
    RelativeLayout rl_need_check_base;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup_other_switch);
        ButterKnife.bind(this);
        new SaveParameterPresenter(this, Repository.getInstance());

        initTitle();
        Bundle extras = getIntent().getExtras();
        if(extras!=null){
            Boolean isshow = extras.getBoolean("isshow",false);
            if(isshow){
                rl_need_check_base.setVisibility(View.VISIBLE);
            }else{
                rl_need_check_base.setVisibility(View.GONE);
            }
        }
    }

    //初始化标题栏
    private void initTitle() {
        new TitleBuilder(this)
                .setExternalTitleBgRes(R.drawable.bg_blue_icon)
                .setLeftImage(R.drawable.arrow_icon)
                .setRightText(getString(R.string.action_save))
                .setTitleText(getString(R.string.other_class_settings))
                .build();
    }

    @Override
    protected HashMap<String, String> getDataBind() {
        HashMap<String, String> map = new HashMap<>();
        map.put(PreferencesConstants.OFFLINE_TRANS_UPLOAD_NUMBER, offlineTransUploadNumber.getText().toString());
        map.put(PreferencesConstants.OFFLINE_TRANS_UPLOAD_TIMES, offlineTransUploadTimes.getText().toString());
        map.put(PreferencesConstants.REVERSAL_UPLOAD_TIMES, reversalUploadTimes.getText().toString());
        String amunt = StringUtils.getFormatAmount(maxRefundAmount.getText().toString());
        map.put(PreferencesConstants.MAX_REFUND_AMOUNT, amunt);
        map.put(PreferencesConstants.VOID_NEED_PASSWORD, voidNeedPassword.isChecked() ? "1" : "0");
        map.put(PreferencesConstants.PRE_AUTH_VOID_NEED_PASSWORD, preAuthVoidNeedPassword.isChecked() ? "1" : "0");
        map.put(PreferencesConstants.PRE_AUTH_COMPLETE_NEED_PASSWORD, preAuthCompleteNeedPassword.isChecked() ? "1" : "0");
        map.put(PreferencesConstants.PRE_AUTH_COMPLETE_VOID_NEED_PASSWORD, preAuthCompleteVoidNeedPassword.isChecked() ? "1" : "0");
        map.put(PreferencesConstants.ALLOW_MANUAL_PAN, allowManualPan.isChecked() ? "1" : "0");
        map.put(PreferencesConstants.IS_NEED_CONFIRM_CARD_NO, confirmPan.isChecked() ? "1" : "0");
        map.put(PreferencesConstants.NEED_CHECK_BASE, checkGps.isChecked() ? "1" : "0");
        return map;
    }

    @Override
    public void showDetail() {
        offlineTransUploadNumber.setText(mPresenter.getParamValue(PreferencesConstants.OFFLINE_TRANS_UPLOAD_NUMBER));
        offlineTransUploadTimes.setText(mPresenter.getParamValue(PreferencesConstants.OFFLINE_TRANS_UPLOAD_TIMES));
        reversalUploadTimes.setText(mPresenter.getParamValue(PreferencesConstants.REVERSAL_UPLOAD_TIMES));
        maxRefundAmount.setText(StringUtils.formatAmount(mPresenter.getParamValue(PreferencesConstants.MAX_REFUND_AMOUNT), SysConstant.FUND_DIGITS));
        voidNeedPassword.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.VOID_NEED_PASSWORD)));
        preAuthVoidNeedPassword.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.PRE_AUTH_VOID_NEED_PASSWORD)));
        preAuthCompleteNeedPassword.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.PRE_AUTH_COMPLETE_NEED_PASSWORD)));
        preAuthCompleteVoidNeedPassword.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.PRE_AUTH_COMPLETE_VOID_NEED_PASSWORD)));
        allowManualPan.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.ALLOW_MANUAL_PAN)));
        confirmPan.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.IS_NEED_CONFIRM_CARD_NO)));
        maxRefundAmount.addTextChangedListener(new AmountTextWatcher(maxRefundAmount));
        checkGps.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.NEED_CHECK_BASE)));

    }

    @OnClick({R.id.title_iv_left, R.id.title_tv_right})
    public void onClicked(View view) {
        if (UIUtils.isDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.title_iv_left:
                finish();
                break;
            case R.id.title_tv_right:
                if (mPresenter != null) {
                    mPresenter.saveParams(getDataBind());
                }
                break;
            default:
                break;
        }
    }

    public static Intent newInstance(Context context) {
        return new Intent(context, SetupOtherSwitchActivity.class);
    }
}
